using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using ProjectSurvivor;
using QFramework;
using UnityEngine;

namespace  ProjectSurvivor
{
    public class Global:Architecture<Global>
    {
        #if  UNITY_EDITOR
        [UnityEditor.MenuItem("Tool/Clear All Data")]
        public static void ClearAllData()
        {
            PlayerPrefs.DeleteAll();
        }
        #endif
        
        #region Model
        
        //生命值
        public static BindableProperty<int> Hp = new BindableProperty<int>(3);
        //最大生命值
        public static BindableProperty<int> MaxHp = new BindableProperty<int>(3);
        //经验值
        public static BindableProperty<int> Exp = new BindableProperty<int>(0);
        //金币
        public static BindableProperty<int> Coin = new BindableProperty<int>(0);
        //等级
        public static BindableProperty<int> Level = new BindableProperty<int>(1);
        //时间
        public static BindableProperty<float> CurrentSeconds = new BindableProperty<float>(0);
        //是否有普通剑能力
        public static BindableProperty<bool> SimpleSwordUnlocked = new BindableProperty<bool>(false);
        //剑的攻击力
        public static BindableProperty<float> SimpleAbilityDamage = new BindableProperty<float>(Config.InitSimpleSwordDamage);
        //剑的攻击间隔
        public static BindableProperty<float> SimpleAbilityDuration = new BindableProperty<float>(Config.InitSimpleSwordDuration);
        //剑的数量
        public static BindableProperty<int> SimpleSwordCount = new BindableProperty<int>(Config.InitSimpleSwordCount);
        //剑的范围
        public static BindableProperty<float> SimpleSwordRange = new BindableProperty<float>(Config.InitSimpleSwordRange);
        //是否有炸弹
        public static BindableProperty<bool> BombUnlocked=new BindableProperty<bool>(false);    
        //炸弹变量
        public static BindableProperty<float> BombDamage=new BindableProperty<float>(Config.InitBombDamage);
        public static BindableProperty<float> BombPercent=new BindableProperty<float>(Config.InitBombPercent);
        
        
        //是否有小刀能力
        public static BindableProperty<bool> SimpleKnifeUnlocked = new BindableProperty<bool>(false);
        //小刀的攻击力
        public static BindableProperty<float> SimpleKnifeDamage=new BindableProperty<float>(Config.InitSimpleKnifeDamage);
        //小刀的数量
        public static BindableProperty<int> SimpleKnifeCount=new BindableProperty<int>(Config.InitSimpleKnifeCount);
        //小刀的攻击间隔
        public static BindableProperty<float> SimpleKnifeDuration=new BindableProperty<float>(Config.InitSimpleKnifeDuration);
        //穿透数量
        public static BindableProperty<int> SimpleKnifeAttackCount=new BindableProperty<int>(1);

        
        
        public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.3f);
        public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.05f);
        
        //是否有守卫剑能力
        public static BindableProperty<bool> RotateSwordUnlocked = new BindableProperty<bool>(false);
        public static BindableProperty<float> RotateSwordDamage=new(Config.InitRotateSwordDamage);
        public static BindableProperty<int> RotateSwordCount=new(Config.InitRotateSwordCount);
        public static BindableProperty<float> RotateSwordSpeed=new(Config.InitRotateSwordSpeed);
        public static BindableProperty<float> RotateSwordRange = new(Config.InitRotateSwordRange);
        
        //是否有篮球能力
        public static BindableProperty<bool> BasketBallUnlocked=new BindableProperty<bool>(false);
        public static BindableProperty<float> BasketBallDamage=new(Config.InitBasketBallDamage);
        public static BindableProperty<float> BasketBallSpeed=new(Config.InitBasketBallSpeed);
        public static BindableProperty<int> BasketBallCount = new(Config.InitBasketBallCount);
        
        public static BindableProperty<float> CritcalRate = new BindableProperty<float>(Config.InitCriticalRate);
        public static BindableProperty<float> DamageRate=new BindableProperty<float>(1);
        public static BindableProperty<int> AdditionFlyThingCount=new (0);
        
        //移动速度
        public static BindableProperty<float> MovementSpeedRate=new(1.0f);
        //收取范围
        public static BindableProperty<float> CollectableArea=new(Config.InitCollectableArea);
        //
        public static BindableProperty<float> AdditionalExpPercent=new(0);
        
        
        public static BindableProperty<bool> SuperRotateSward=new (false);
        public static BindableProperty<bool> SuperKnife=new (false);
        public static BindableProperty<bool> SuperSword=new (false);
        public static BindableProperty<bool> SuperBomb=new (false);
        public static BindableProperty<bool> SuperBasketBall=new (false);
        #endregion
        

        [RuntimeInitializeOnLoadMethod]
        //开始游戏的时候从本地取出永久值并赋值
        public static void AutoInit()
        {
            AudioKit.PlaySoundMode = AudioKit.PlaySoundModes.IgnoreSameSoundInGlobalFrames;
            ResKit.Init();
            UIKit.Root.SetResolution(1920,1080,1);
            Global.MaxHp.Value=PlayerPrefs.GetInt(nameof(MaxHp),MaxHp.Value);
            Hp.Value = MaxHp.Value;
            Global.Coin.Value = PlayerPrefs.GetInt(nameof(Coin),0);
            
            Global.ExpPercent.Value = PlayerPrefs.GetFloat(nameof(ExpPercent),0.4f);
            Global.CoinPercent.Value = PlayerPrefs.GetFloat(nameof(CoinPercent),0.1f);
            Global.Coin.Register(coin =>
            {
                PlayerPrefs.SetInt(nameof(Coin),coin);
            });
           
            Global.ExpPercent.Register(expPercent =>
            {
                PlayerPrefs.SetFloat(nameof(ExpPercent),expPercent);
            });
            
            Global.CoinPercent.Register(coinPercent =>
            {
                PlayerPrefs.SetFloat(nameof(CoinPercent),coinPercent);
            });

            Global.MaxHp.Register(maxhp =>
            {
                PlayerPrefs.SetInt(nameof(MaxHp),maxhp);
            });

            var _ = Interface;
        }
        //游戏重新开始后将不是永久值的值赋初始值
        public static void ResetData()
        {
            Hp.Value = MaxHp.Value;
            Exp.Value = 0;
            Level.Value = 1;
            CurrentSeconds.Value = 0;

            SimpleSwordUnlocked.Value = false;
            SimpleAbilityDamage.Value = Config.InitSimpleSwordDamage;
            SimpleAbilityDuration.Value =Config.InitSimpleSwordDuration;
            SimpleSwordCount.Value =Config.InitSimpleSwordCount;
            SimpleSwordRange.Value =Config.InitSimpleSwordRange;

            BombPercent.Value = Config.InitBombPercent;
            BombUnlocked.Value = false;
            BombDamage.Value = Config.InitBombDamage;

            SimpleKnifeUnlocked.Value = false;
            SimpleKnifeDuration.Value = Config.InitSimpleKnifeDuration;
            SimpleKnifeDamage.Value = Config.InitSimpleKnifeDamage;
            SimpleSwordCount.Value = Config.InitSimpleKnifeCount;
            SimpleKnifeAttackCount.Value = 1;

            RotateSwordUnlocked.Value = false;
            RotateSwordDamage.Value = Config.InitRotateSwordDamage;
            RotateSwordCount.Value = Config.InitRotateSwordCount;
            RotateSwordRange.Value = Config.InitRotateSwordRange;
            RotateSwordSpeed.Value = Config.InitRotateSwordSpeed;

            BasketBallUnlocked.Value = false;
            BasketBallDamage.Value = Config.InitBasketBallDamage;
            BasketBallCount.Value = Config.InitBasketBallCount;
            BasketBallSpeed.Value = Config.InitBasketBallSpeed;

            CritcalRate.Value = Config.InitCriticalRate;
            DamageRate.Value = 1;
            AdditionFlyThingCount.Value = 0;

            MovementSpeedRate.Value = 1.0f;
            CollectableArea.Value = Config.InitCollectableArea;
            AdditionalExpPercent.Value = 0;
            
            EnemyGenerator.EnemyCount.Value = 0;

            SuperRotateSward.Value = false;
            SuperBasketBall.Value = false;
            SuperKnife.Value = false;
            SuperSword.Value = false;
            SuperBomb.Value = false;
            Interface.GetSystem<ExpUpgradeSystem>().ResetData();
        }
        
        //获取经验升级公式
        public static int ExpToNextLevel()
        {
            return Level.Value * 5;
        }
        
        //敌人死后随机生成道具
        public static void GeneratePowerUp(GameObject gameObject,bool gentreasureChest)
        {
            if (gentreasureChest)
            {
                PoweUpManager.Default.Treasuerchest
                    .Instantiate()
                    .Position(gameObject.Position())
                    .Show();
                return;
            }
            //%90掉落经验值
            var percent = Random.Range(0,1f);
            if (percent < ExpPercent.Value+AdditionalExpPercent.Value)
            {
                //掉落经验值
                PoweUpManager.Default.Exp.Instantiate()
                    .Position(gameObject.Position()) 
                    .Show();
                return;
            }
            percent = Random.Range(0,1f);
            if (percent < CoinPercent.Value)
            {
                PoweUpManager.Default.Coin.Instantiate()
                    .Position(gameObject.Position())
                    .Show();
                
                return;
            }
            percent = Random.Range(0,1f);
            if (percent < 0.1f && !Object.FindObjectOfType<Hp>())
            {
                PoweUpManager.Default.Hp.Instantiate()
                    .Position(gameObject.Position())
                    .Show();
                
                return;
            }

            if (BombUnlocked.Value && !Object.FindObjectOfType<Bomb>())
            {
                percent = Random.Range(0,1f);
                if (percent < BombPercent.Value)
                {
                    PoweUpManager.Default.Bomb.Instantiate()
                        .Position(gameObject.Position())
                        .Show();
                
                    return;
                }
            }
            
            percent = Random.Range(0,1f);
            if (percent < 0.1f&& !Object.FindObjectOfType<GetAllExp>())
            {
                PoweUpManager.Default.GetAllExp.Instantiate()
                    .Position(gameObject.Position())
                    .Show();
                
                return;
            }

            
        }

        protected override void Init()
        {
            //注册模块的操作
            //XXX Model
            this.RegisterSystem(new SaveSystem());
            this.RegisterSystem(new CoinUpgradeSystem());
            this.RegisterSystem(new ExpUpgradeSystem());
            this.RegisterSystem(new AchievementSystem());
        }
    }
}